﻿/******************************************************************************
* File description: updateExe.cpp
* Function: 更新软件
* Current Version: 1.0.0
* Author: JNN
* Date: 2025-07-01
*  -----
*  Copyright by 博坤生物 .
 *****************************************************************************/
#include "updateExe.h"

UpdateExe::UpdateExe()
{

}

/** @brief 从服务器端更新软件 */
QString UpdateExe::updateFromServer(){


#ifdef Q_OS_WINDOWS
    int downloadRes = DownloadManager::I().downloadFile("http://8.137.108.100:8081/nuclide.exe", "./newFilePath.exe", true);
    if(downloadRes == -1){
        return "服务器连接失败";
    }else if(downloadRes == -2){
        return "下载文件失败，请检查本地文件权限配置";
    }
    _newFilePath = "./newFilePath.exe";
    WinExec(("update.exe " + _newFilePath + " NIDCS.exe").toLatin1(), SW_SHOWNORMAL);
    QFile::copy(":/src/update.exe", "./update.exe");
    exit(0);
    return "";
#endif

    return "当前系统暂不支持自动更新";
}


/** @brief 选择本地文件路径 */
QString UpdateExe::selectFilePath(){

#ifdef Q_OS_WINDOWS
    _newFilePath = QFileDialog::getOpenFileName(nullptr, tr("Open Update File"), "", tr("Excel Files (*.exe)"));
    return _newFilePath;
#else
    return "";
#endif
}


/** @brief 更新软件 */
QString UpdateExe::execUpdate(){

#ifdef Q_OS_WINDOWS
    if(!QFile::exists(_newFilePath)){
        return "选择的文件无效";
    }

    QFile::copy(":/src/update.exe", "./update.exe");
    WinExec(("update.exe " + _newFilePath + " NIDCS.exe").toLatin1(), SW_SHOWNORMAL);

    exit(0);
    return "";
#endif
    return "当前系统暂不支持自动更新";

}
